package kr.co.valueworks.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.location.Location;

public class GoogleReverseGeocoder
{
	//
	private static  GoogleReverseGeocoder instance = null;
	public String _strAddress;
	
	public GoogleReverseGeocoder()
	{
	}
	
	static public GoogleReverseGeocoder getInstance()
	{
		if(instance == null)
		{
			instance = new GoogleReverseGeocoder();
		}
		return instance;
	}
	
	public void startGeocodeAddress(Location loc)
	{
		URL url;
		try 
		{
			url = new URL(String.format("http://maps.google.com/maps/geo?q=%f,%f&sensor=false&output=xml", loc.getLatitude(), loc.getLongitude()));
			URLConnection urlConn = url.openConnection();
			InputStream input = urlConn.getInputStream();
		
			BufferedReader in = new BufferedReader(new InputStreamReader(input));
			String currLine = new String();
			StringBuffer receiveMsg = new StringBuffer();
			while((currLine = in.readLine()) != null){
				receiveMsg.append(currLine).append("\r\n");
			}
			/*
			int iPacketLength = input.available();
			byte[] baBuffer = new byte[iPacketLength];
			int readBytes = -1;
			int offset = 0;
			while((readBytes = input.read(baBuffer, offset, iPacketLength-offset)) != 0)
			{
				offset+=readBytes;
			}   
			String strXml = new String(baBuffer, 0, iPacketLength);
			*/
			
		
			//
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(new StringReader(receiveMsg.toString()));
		
			int parserEvent = parser.getEventType();
			
			int iStartParsing = 0;
			String strTag = null;
			_strAddress = "";
			
			while((parserEvent = parser.next()) != XmlPullParser.END_DOCUMENT)
			{
			
				switch (parserEvent)
				{
					case XmlPullParser.START_DOCUMENT:
					// 문서의 시작. 별로 할일 없으니 다음(이벤트)
					break;
					
					case XmlPullParser.END_DOCUMENT:
					//문서의 끝. 별로 할일 없으니 다음(이벤트)
					break;
				
					case XmlPullParser.START_TAG: //시작이벤트를 만나고"<>"
					strTag = parser.getName();
					//      Log.v(" " + parserEvent, strTag);
				
					if (strTag.equals("Placemark"))
					{
						for (int i = 0; i < parser.getAttributeCount(); i++)
						{
							if (parser.getAttributeValue(0).equals("p1"))
							{
								iStartParsing = 1;
								break;
							}
							else
							{
								iStartParsing = 0;
							}
						}
					}
					break;                      
				
					case XmlPullParser.END_TAG: //엔드태그"</>" 를 만났을때
					strTag = parser.getName(); 
					//      Log.v(" " + parserEvent, strTag);
					break;
				
					case XmlPullParser.TEXT:
					String strText = parser.getText();
				
					if (0 != iStartParsing) 
					{
						if (strTag.equals("LocalityName"))
						{
							_strAddress = strText;
						}
						else if(strTag.equals("DependentLocalityName") ||
						strTag.equals("ThoroughfareName"))
						{
							_strAddress += " ";
							_strAddress += strText;
						}
					}     
					break;
				}
			}
		
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
